package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.mapper.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.BookingRule;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author:ahang
 * @create:2022-09-01 20:20
 * @Description:
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;


    /**
     * 添加/修改医院信息
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) {
        //1.map ---> hospital
        String jsonString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        //2.根据hoscode从mongodb中查询医院是否存在
        Hospital byHoscode = hospitalRepository.findByHoscode(hospital.getHoscode());
        //3.判断  不存在--添加 存在--修改
        if(byHoscode == null){
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setStatus(1);
            hospitalRepository.insert(hospital);
        }else{
            hospital.setUpdateTime(new Date());
            hospital.setStatus(1);
            hospitalRepository.save(hospital);
        }

    }

    /**
     * 根据医院编码获取医院信息
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }

    /**
     * 分页+条件 查询
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //排序
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        PageRequest pageRequest = PageRequest.of(page-1,limit,sort);

        //将传递的条件封装到hospital中
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreCase(true).withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        Example<Hospital> example = Example.of(hospital,matcher);

        Page<Hospital> pageResult = hospitalRepository.findAll(example, pageRequest);

        pageResult.getContent().forEach(item->{
            this.packHospital(item);
        });
        return pageResult;
    }

    //远程调用
    @Autowired
    private DictFeignClient dictFeignClient;
    //对医院对象进行封装(param中封装数据)
    public Hospital packHospital(Hospital hospital){
        String hostype = hospital.getHostype(); //医院等级 value
        String provinceCode = hospital.getProvinceCode();// 省 value
        String cityCode = hospital.getCityCode();// 市 value
        String districtCode = hospital.getDistrictCode(); //区  value

        //远程调用feign
        String hostypeString = dictFeignClient.getName("Hostype", hostype);
        String provinceString = dictFeignClient.getName(provinceCode);
        String cityString = dictFeignClient.getName(cityCode);
        String districtString = dictFeignClient.getName(districtCode);

        //封装到param中
        String fullAddress = provinceString +cityString + districtString + hospital.getAddress();
        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",fullAddress);

        return hospital;

    }

    /**
     * 修改医院状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        Optional<Hospital> byId = hospitalRepository.findById(id);
        Hospital hospital = byId.get();
        hospital.setStatus(status);
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> show(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.packHospital(hospital);  //引用

        BookingRule bookingRule = hospital.getBookingRule();

        Map<String,Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",bookingRule);
        return map;

    }

    //根据医院编号获取医院姓名
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";
    }

    //根据医院名称模糊查询
    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> list = hospitalRepository.findHospitalByHosnameLike(hosname);
        return list;
    }

    //根据医院编号获取医院详情
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String,Object> map = new HashMap<>();
        Hospital hospital = packHospital(this.getByHoscode(hoscode));

        //封装map,将医院详情和bookingRule分开
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        map.put("hospital",hospital);
        return map;
    }


}
